#include "../mqserver/exchange.hpp"
#include <gtest/gtest.h>

ExchangeManager::ptr emp;

class ExchangeTest : public testing::Environment {
public:
    virtual void SetUp() override {
        emp = std::make_shared<ExchangeManager>("./data/meta.db");
    }
    virtual void TearDown() override {
        emp->clear();//把这个注释掉才能看到数据库的内容哦
    }
};

TEST(ExchangeTest, insert_test){
    std::unordered_map<std::string, std::string> map = {{"k1", "v1"}, {"k2", "v2"}};
    emp->declareExchange("exchange1", mymq::ExchangeType::DIRECT, true, false, map);
    emp->declareExchange("exchange2", mymq::ExchangeType::DIRECT, true, false, map);
    emp->declareExchange("exchange3", mymq::ExchangeType::DIRECT, true, false, map);
    emp->declareExchange("exchange4", mymq::ExchangeType::DIRECT, true, false, map);
    ASSERT_EQ(emp->size(), 4);
}

TEST(exchangeTest, select_test){
    Exchange::ptr exch = emp->selectExchange("exchange3");
    ASSERT_EQ(exch->name, "exchange3");
    ASSERT_EQ(exch->durable, true);
    ASSERT_EQ(exch->auto_delete, false);
    ASSERT_EQ(exch->type, mymq::ExchangeType::DIRECT);
    ASSERT_EQ(exch->getArgs(), std::string("k1=v1&k2=v2&"));
}

TEST(exchangeTest, remove_test){
    emp->deleteExchange("exchange3");
    Exchange::ptr exch = emp->selectExchange("exchange3");
    ASSERT_EQ(exch.get(), nullptr);
    ASSERT_EQ(emp->exists("exchange3"), false);
}

int main(int argc, char* argv[]){
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ExchangeTest);
    RUN_ALL_TESTS();
    return 0;
}